package com.lxw.redis.study.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lxw.redis.study.core.MyResponse;
import com.lxw.redis.study.entity.Dept;
import com.lxw.redis.study.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;

@RestController
@RequestMapping("/dept")
public class DeptController {

	@Autowired
	DeptService deptService;

	/**
	 * 新增
	 * @param dept
	 * @return
	 */
	@PostMapping("/dept")
	public MyResponse insert(@RequestBody Dept dept) {
		deptService.insert(dept);
		String msg = dept.getMsg();
		if (StringUtils.isEmpty(msg)) {
			msg = "新增成功";
		}
		return MyResponse.success().setMessage(msg);
	}

	/**
	 * 根据主键删除
	 * @param id
	 * @return
	 */
	@DeleteMapping("/dept/{id}")
	public MyResponse deleteByPrimaryKey(@PathVariable("id") Integer id) {
		deptService.deleteByPrimaryKey(id);
		return MyResponse.success().setMessage("删除成功");
	}

	/**
	 * 根据主键更新
	 * @param dept
	 * @return
	 */
	@PutMapping("/dept")
	public MyResponse update(@RequestBody Dept dept) {
		deptService.update(dept);
		String msg = dept.getMsg();
		if (StringUtils.isEmpty(msg)) {
			msg = "修改成功";
		}
		return MyResponse.success().setMessage(msg);
	}

	/**
	 * 根据主键查找
	 * @param id 主键
	 * @return
	 */
	@GetMapping("/dept/{id}")
	public MyResponse selectByPrimaryKey(@PathVariable("id") Integer id) {
		Dept dept = deptService.selectByPrimaryKey(id);
		return MyResponse.success(dept);
	}

	/**
	 * 分页查询
	 * @param param
	 * @return
	 */
	@PostMapping("/getDeptListByPage")
	public MyResponse selectList(@RequestBody Dept param) {
		Integer currentPage = param.getCurrentPage();
		Integer pageSize = param.getPageSize();
		PageHelper.startPage(currentPage, pageSize);
		List<Dept> depts = deptService.selectList(param);
		return MyResponse.success(new PageInfo<>(depts));
	}

}
